package com.triones.ljr.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtil;
import com.triones.ljr.entity.dto.*;
import com.triones.ljr.entity.vo.OvertDeptVO;
import com.triones.ljr.entity.vo.OvertEnterpriseVO;
import com.triones.ljr.mapper.OvertDeptMapper;
import com.triones.ljr.mapper.OvertEnterpriseMapper;
import com.triones.ljr.service.OvertService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 企业公开
 */
@Service
@RequiredArgsConstructor
public class OvertServiceImpl implements OvertService {

    private final OvertDeptMapper overtDeptMapper;

    private final OvertEnterpriseMapper overtEnterpriseMapper;

    @Override
    public TableDataInfo<OvertDeptVO> deptPageList(OvertDeptDO dto, PageQuery pageQuery) {
        LambdaQueryWrapper<OvertDeptDO> lqw = Wrappers.lambdaQuery();
        lqw.eq(OvertDeptDO::getIsDel,"0")
            .like(ObjectUtil.isNotEmpty(dto.getDeptName()),OvertDeptDO::getDeptName,dto.getDeptName())
            .orderByAsc(OvertDeptDO::getSort);
        IPage<OvertDeptVO> result = overtDeptMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public OvertDeptVO getDeptInfo(Long id) {
        return overtDeptMapper.selectVoById(id);
    }

    @Override
    public boolean checkDeptNameUnique(OvertDeptDO dto) {
        boolean exist = overtDeptMapper.exists(new LambdaQueryWrapper<OvertDeptDO>()
            .eq(OvertDeptDO::getDeptName, dto.getDeptName())
            .eq(OvertDeptDO::getIsDel, "0")
            .ne(ObjectUtil.isNotNull(dto.getId()), OvertDeptDO::getId, dto.getId()));
        return !exist;
    }

    @Override
    public boolean addDept(OvertDeptDO dto) {
        dto.setCreateTime(DateUtil.getDateTimeString());
        return overtDeptMapper.insert(dto) > 0;
    }

    @Override
    public boolean editDept(OvertDeptDO dto) {
        return overtDeptMapper.updateById(dto) > 0;
    }

    @Override
    public boolean removeDept(Long id) {
        OvertDeptDO overtDeptDO = overtDeptMapper.selectById(id);
        if (overtDeptDO == null) {
            throw new SecurityException("没有查询到信息");
        }
        Long selectCount = overtEnterpriseMapper.selectCount(new LambdaQueryWrapper<OvertEnterpriseDO>()
            .eq(OvertEnterpriseDO::getDid, id)
            .eq(OvertEnterpriseDO::getIsDel, "0"));
        if (selectCount > 0) {
            throw new ServiceException("该部门下有企业不允许删除");
        }
        overtDeptDO.setIsDel("1");
        return overtDeptMapper.updateById(overtDeptDO) > 0;
    }

    @Override
    public List<OvertDeptVO> selectDeptAll(String keyword) {
        return overtDeptMapper.selectVoList(new LambdaQueryWrapper<OvertDeptDO>()
            .like(ObjectUtil.isNotEmpty(keyword),OvertDeptDO::getDeptName,keyword)
            .eq(OvertDeptDO::getIsDel,"0").orderByAsc(OvertDeptDO::getSort));
    }

    @Override
    public TableDataInfo<OvertEnterpriseVO> enterprisePageList(OvertEnterpriseDO dto, PageQuery pageQuery) {
        QueryWrapper<Object> wrapper = Wrappers.query();
        wrapper.eq("toe.is_del","0")
            .eq(ObjectUtil.isNotNull(dto.getDid()),"toe.did",dto.getDid())
            .like(ObjectUtil.isNotEmpty(dto.getName()),"toe.name",dto.getName()).or()
            .like(ObjectUtil.isNotEmpty(dto.getCategory()),"toe.category",dto.getCategory())
            .orderByAsc("toe.sort");
        Page<OvertEnterpriseVO> result = overtEnterpriseMapper.selectPageList(pageQuery.build(), wrapper);
        return TableDataInfo.build(result);
    }

    @Override
    public OvertEnterpriseVO getEnterpriseInfo(Long id) {
        return overtEnterpriseMapper.getEnterpriseInfo(id);
    }

    @Override
    public boolean checkEnterpriseNameUnique(OvertEnterpriseDO dto) {
        boolean exist = overtEnterpriseMapper.exists(new LambdaQueryWrapper<OvertEnterpriseDO>()
            .eq(OvertEnterpriseDO::getName, dto.getName())
            .eq(OvertEnterpriseDO::getDid,dto.getDid())
            .eq(OvertEnterpriseDO::getIsDel, "0")
            .ne(ObjectUtil.isNotNull(dto.getId()), OvertEnterpriseDO::getId, dto.getId()));
        return !exist;
    }

    @Override
    public boolean addEnterprise(OvertEnterpriseDO dto) {
        dto.setCreateTime(DateUtil.getDateTimeString());
        return overtEnterpriseMapper.insert(dto) > 0;
    }

    @Override
    public boolean editEnterprise(OvertEnterpriseDO dto) {
        return overtEnterpriseMapper.updateById(dto) > 0;
    }

    @Override
    public boolean removeEnterprise(Long id) {
        OvertEnterpriseDO overtEnterpriseDO = overtEnterpriseMapper.selectById(id);
        if (overtEnterpriseDO == null) {
            throw new SecurityException("没有查询到有效的信息");
        }
        overtEnterpriseDO.setIsDel("1");
        return overtEnterpriseMapper.updateById(overtEnterpriseDO) > 0;
    }

    @Override
    public void saveBatch(List<OvertEnterpriseDO> list) {
        overtEnterpriseMapper.insertBatch(list);
    }

    @Override
    public List<OvertEnterpriseVO> queryList(String keyword) {
        return overtEnterpriseMapper.selectVoList(new LambdaQueryWrapper<OvertEnterpriseDO>()
            .eq(OvertEnterpriseDO::getIsDel,"0")
            .like(ObjectUtil.isNotEmpty(keyword),OvertEnterpriseDO::getName,keyword)
            .orderByAsc(OvertEnterpriseDO::getSort));
    }
}
